package com.itydl.a03qq.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.PixelFormat;
import android.graphics.PointF;
import android.graphics.drawable.AnimationDrawable;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.itydl.a03qq.BubbleUtils;
import com.itydl.a03qq.R;

/**
 * 作者：${Howard} on 2018/2/8 22:12
 *
 * @version 1.0
 *          说明：监听任意View的触摸事件
 */
public class BubbleMesTouchListener implements View.OnTouchListener, MsgBubbleView.OnMsgBubbleStateListener {

    //任意View
    private View mStaticView;
    private Context mContext;
    private OnBubbleDisappear mOnBubbleDisappear;
    private WindowManager mWindowManager;
    private MsgBubbleView mBubbleView;
    private WindowManager.LayoutParams mParams;
    private FrameLayout mBubbleBomContainer;//爆炸imageview的容器
    private ImageView mBubbleBomView;//拖拽爆炸ImageView

    public BubbleMesTouchListener(View view, Context context, OnBubbleDisappear onBubbleDisappear){
        this.mStaticView = view;
        this.mContext = context;
        this.mOnBubbleDisappear = onBubbleDisappear;
        mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        mParams = new WindowManager.LayoutParams();
        mParams.format = PixelFormat.TRANSPARENT;//设置为透明
        mBubbleView = new MsgBubbleView(context);

        mBubbleView.setOnMsgBubbleStateListener(this);

        mBubbleBomContainer = new FrameLayout(mContext);
        mBubbleBomView = new ImageView(mContext);
        FrameLayout.LayoutParams params = new FrameLayout.
                LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mBubbleBomView.setLayoutParams(params);
        mBubbleBomContainer.addView(mBubbleBomView);

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //点击隐藏掉需要拖拽爆炸的View
                mStaticView.setVisibility(View.GONE);
                mWindowManager.addView(mBubbleView,mParams);

                int[] position = new int[2];
                mStaticView.getLocationOnScreen(position);

                //坐标重新计算，解决错位的bug
                mBubbleView.initDownPoint(position[0]+mStaticView.getWidth()/2,
                        position[1]+mStaticView.getHeight()/2- BubbleUtils.getStatusBarHeight(mContext));

                //设置staticView的bitmap给MsgBubbleView，进行绘制
                Bitmap bitmap = getBitmapByView(mStaticView);
                mBubbleView.setBitmap(bitmap);

                break;
            case MotionEvent.ACTION_MOVE:
                float moveX = event.getRawX();
                float moveY = event.getRawY();
                moveY = (moveY - BubbleUtils.getStatusBarHeight(mContext));
                mBubbleView.initDragPoint(moveX,moveY);
                break;
            case MotionEvent.ACTION_UP:
                mBubbleView.handleActionUp();
                break;
        }
        return true;
    }

    /**
     * 根据View获取它的Bitmap实例
     * @param view
     * @return
     */
    private Bitmap getBitmapByView(View view) {
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();
        return bitmap;
    }

    @Override
    public void dismiss(PointF pointF) {
        // 拖拽爆炸
        // 把消息的View移除
        mWindowManager.removeView(mBubbleView);
        // 要在 mWindowManager 添加一个爆炸动画
        mWindowManager.addView(mBubbleBomContainer,mParams);

        // 加载帧动画
        mBubbleBomView.setBackgroundResource(R.drawable.anim_bubble_pop);

        mBubbleBomView.setX(pointF.x - mStaticView.getWidth()/2);
        mBubbleBomView.setY(pointF.y - mStaticView.getHeight()/2);

        AnimationDrawable animationDrawable = (AnimationDrawable) mBubbleBomView.getBackground();
        animationDrawable.start();

        // 等它【执行完】之后我要移除掉这个 爆炸动画   也就是 mBubbleBomContainer
        mBubbleBomView.postDelayed(new Runnable() {
            @Override
            public void run() {
                //移除mBubbleBomContainer
                mWindowManager.removeView(mBubbleBomContainer);
                mBubbleBomContainer.removeView(mBubbleBomView);
                // 通知一下外面该View消失
                if(mOnBubbleDisappear != null){
                    mOnBubbleDisappear.disappear(mStaticView);
                }
            }
        },getAnimationDrawableTime(animationDrawable));
    }

    /**
     * 获取动画执行时间
     * @param drawable
     * @return
     */
    private long getAnimationDrawableTime(AnimationDrawable drawable) {
        int numberOfFrames = drawable.getNumberOfFrames();
        long time = 0;
        for (int i=0;i<numberOfFrames;i++){
            time += drawable.getDuration(i);
        }
        return time;
    }

    @Override
    public void resilience() {
        // 把消息的View移除
        mWindowManager.removeView(mBubbleView);
        // 把原来的View显示
        mStaticView.setVisibility(View.VISIBLE);
    }

    public interface OnBubbleDisappear{
        /**
         * 当前View拖拽消失回调接口
         * @param view
         */
        void disappear(View view);
    }
}
